package com.shelpe.services.sinbad.repository.jpa;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.DriveType;
import com.shelpe.services.sinbad.entity.jpa.OnlineStatus;

public interface AdgroupRepository
		extends PagingAndSortingRepository<Adgroup, Long>, JpaSpecificationExecutor<Adgroup> {

	List<Adgroup> findByCampaignID(Long campaignID);

	List<Adgroup> findByCampaignIDIn(List<Long> campaignIDs);

	List<Adgroup> findByAutoIDIn(List<Long> autoIDs);

	List<Adgroup> findByUserID(long userID);

	List<Adgroup> findByCreateTimeAfter(Date createTime);

	long countByCampaignID(long campaignID);

	List<Adgroup> findByCampaignIDAndDriveTypeAndOnlineStatusOrderByCreateTime(Long campaignID, DriveType driveType,
			OnlineStatus onlineStatus);

	List<Adgroup> findByCampaignIDAndProductIDIn(long campaignID, List<Long> productIDs);

	@Query("select distinct po.productID from adgroups po where po.campaignID = :campaignID ")
	List<Long> findProductIDsByCampaignID(@Param("campaignID") long campaignID);

	@Query("select distinct po.productID from adgroups po where po.campaignID IN :ids")
	List<Long> findProductIDsByCampaignIDs(@Param("ids") List<Long> campaignIDs);
	
	@Query("select distinct po.autoID from adgroups po where po.userID IN :ids")
	List<Long> findAdgroupIDsByUserID(@Param("ids") List<Long> userIDs);
	
	@Query("select distinct po.autoID from adgroups po where po.userID IN :ids and po.driveType IN :driveTypes")
	List<Long> findAdgroupIDsAndDriveTypeByUserID(@Param("ids") List<Long> userIDs,@Param("driveTypes") List<DriveType> driveTypes);
}